package com.example.demo.moduel.entity;

import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import javax.persistence.*;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Data
@Entity(name = "t_student")
public class Student implements UserDetails, Serializable {
       @Id
       private Long id;
       @Column(name = "user_name")
       private String username;
       @Column
       //@Transient  是否是表字段
       private String password;
       @Column()
       @ManyToMany(cascade = {CascadeType.REFRESH},fetch = FetchType.EAGER)
       private List<Role> roles;

       @Override
       public Collection<? extends GrantedAuthority> getAuthorities() {
              List<GrantedAuthority> auths = new ArrayList<>();
              List<Role> roles = this.getRoles();
              for (Role role : roles) {
                     auths.add(new SimpleGrantedAuthority(role.getRoleName()));
              }
              return auths;
       }

       @Override
       public boolean isAccountNonExpired() {
              return true;
       }

       @Override
       public boolean isAccountNonLocked() {
              return true;
       }

       @Override
       public boolean isCredentialsNonExpired() {
              return true;
       }

       @Override
       public boolean isEnabled() {
              return true;
       }
}
